import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/controller/schooladmin.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/pages/teacher/announcementNew.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/announcementDataGrid.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import '../../themes/pageStyles.dart';

class AnnouncementsManagement extends StatefulWidget {
  static const String routeName = '/teacher/class/newannouncement';

  @override
  State<AnnouncementsManagement> createState() =>
      _AnnouncementsManagementState();
}

class _AnnouncementsManagementState extends State<AnnouncementsManagement> {
  final box = Hive.box('appconfig');
  final UserController _userController = Get.find();
  final SchoolAdminController _schoolAdminController = Get.find();

  @override
  void initState() {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    _schoolAdminController.announcementItems.clear();
    if (isTeacher) {
      _schoolAdminController.fetchClassAnnouncementsByClassId(
          _userController.currentClass.value.classId.toString());
      // Future.delayed(Duration(milliseconds: 200), (() {
      //   _schoolAdminController.fetchClassAnnouncementsByClassId(
      //       _userController.currentClass.value.classId.toString());
      // }));
    }
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher announcementsManagement');
    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
      backgroundColor: Get.isDarkMode ? Colors.black : Colors.transparent,
      appBar: CustomAppBar(title: 'Announcements'.tr),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_userController.currentClass.value.classId != null) {
            Get.to(() => TeacherCreateAnnouncement());
          }
        },
        backgroundColor: Get.theme.primaryColorDark,
        child: const Icon(Icons.add),
        tooltip: 'Create announcement'.tr,
      ),
      body: Container(
        decoration: _pageStyles.pageBoxDecoration(),
        padding: EdgeInsets.all(15.0),
        child: Container(
          decoration: _pageStyles.dataGridBackground(),
          child: Container(
            margin: EdgeInsets.all(12.0),
            decoration: _pageStyles.dataGridForground(),
            child: _schoolAdminController.obx(
              (data) =>AnnouncementsDataGrids(
                    _userController.currentClass.value,
                    _schoolAdminController.announcementItems),
              
              onEmpty: Center(
                child: Text('You have not created any announcement!'.tr),
              ),
              onLoading: Container(),
              // onLoading: Center(
              //     child: CircularProgressIndicator(
              //   backgroundColor: Get.theme.primaryColorDark,
              //   strokeWidth: 9.0,
              // )),
            ),
          ),
        ),
      ),
    );
  }
}
